package com.zzzyh.forumsystemclient.cotroller;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @PostMapping("/upload")
    public AppResult upload(@RequestParam("file") MultipartFile file,
                            @RequestParam("source") String source) {
        log.info("上传文件: {}", file.getOriginalFilename());
        log.info("source" + source);
        try {
            // 调用上传工具方法
            String fileUrl = FileUtils.uploadUtils(file, source);
            log.info("上传成功: {}", fileUrl);
            return AppResult.success("上传成功", fileUrl);
        } catch (Exception e) {
            log.error("上传失败", e);
            return AppResult.failed("上传失败: " + e.getMessage());
        }
    }


}
